/**
 * 公共消息提示框插件
 * 定义函数用于构造，来传递参数
 * @author lee
 */
var ShowAlertDialog = function(opt) {
    //默认配置参数
    this.opt = {
		msg:"这里是消息内容", //提示消息内容
		title:"提示框",     //弹出框标题
		icon:"info",      //图标:info-普通提示,error-错误提示,warn-警告提示
		okButton:"确认",   //确认按钮内容
		okCallback:null   //确认回调函数
    };
    
    //如果用户输入参数，将参数扩展
    if (opt && $.isPlainObject(opt)) {
        $.extend(this.opt, opt);
    }
    
    //给函数定义变量，并和opt对象一起传入原型
    this.body = $("body");
    this.dialog = $("<div class=\"modal modal-tahoe dialog-tahoe\" tabindex=\"-1\"></div>");
};

//原型中定义函数
ShowAlertDialog.prototype = {
    init:function() {
        //1.this指的就是该原型对象
        //2.this.xxx表示原型对象调用函数中的变量
        var _this_ = this,
            opt = this.opt,
            body = this.body,
            dialog = this.dialog;
        
        var timestamp = new Date().getTime();
        var dialog_id = "msg_dialog_"+timestamp;
        dialog.attr("id",dialog_id);

        var content = "<div class=\"modal-dialog modal-sm\">";
        	content +="	 <div class=\"modal-content\">";
        	content +="	 	<div class=\"modal-header\">";
        	content +="	 		<button data-id=\"close\" class=\"close icon icon-close\"></button>";
        	content +="	 		<h5 data-id=\"title\" class=\"modal-title\">";
        	content +="	 		</h5>";
        	content +="	 	</div>";
        	content +="	 	<div class=\"modal-body tc pt25\">";
//        	content +="	 		<i data-id=\"icon\" class=\"icon\"></i>";
        	content +="	 		<p data-id=\"msg\" class=\"inline_block\">";
        	content +="	 		</p>";
        	content +="	 	</div>";
        	content +="	 	<div class=\"modal-footer tc\">";
        	content +="	 		<button data-id=\"ok\" type=\"button\" class=\"btn\" yesbtn=\"true\">确定</button>";
        	content +="	 	</div>";
        	content +="	 </div>";
        	content +="</div>";
        	
        dialog.html(content);
        body.append(dialog); 
        
        var iconEl = dialog.find("i[data-id='icon']");
        var okBtn = dialog.find("button[data-id='ok']");
        var closeBtn = dialog.find("button[data-id='close']");
        var titleEl = dialog.find("h5[data-id='title']");
        var msgEl = dialog.find("p[data-id='msg']");
        
        //设置图标
        if(opt.icon=="info"){
        	$(iconEl).addClass("icon-success");
        }else if(opt.icon=="error"){
        	$(iconEl).addClass("icon-close colorGrey");
        }else if(opt.icon=="warn"){
        	//TODO:暂无警告图标
        }
        
        //设置标题
        if(opt.title){
        	$(titleEl).html(opt.title);
        }
        
        //设置提示内容
        if(opt.msg){
        	$(msgEl).html(opt.msg);
        }
        
        //设置确认按钮
        if(opt.okButton){
        	$(okBtn).text(opt.okButton);
        }
        
        
        
        $(okBtn).click(function(event){
			_this_.ok();
		});
        
        $(closeBtn).click(function(event){
			_this_.ok();
		});
    },
    checkResource:function(){
    },
    //关闭弹出框
    close: function() {
        this.dialog.modal("hide");
        $(this.dialog).remove();
    },
    //显示弹出框
    show:function(){
    	var _this_ = this,
        dialog = this.dialog;
    	
    	_this_.init();
    	dialog.modal("show");
    },
    ok:function() {
    	var _this_ = this,
        opt = this.opt,
        body = this.body,
        dialog = this.dialog;
    	
    	_this_.close();
    	
    	if(opt.okCallback){
    		opt.okCallback();
    	}
    }
};

//以下是使用demo1
function showAlert1() {
	var dialog = new ShowAlertDialog();
	dialog.show();
}

//以下是使用demo2
function showAlert2() {
	var dialog = new ShowAlertDialog({
		icon:"info",
    	title:"提示框",
		msg:"操作成功!",
		okButton:"确定"
    });
	dialog.show();
}

//以下是使用demo3
function showAlert3() {
	var dialog = new ShowAlertDialog({
		icon:"error",
    	title:"保存提示框",
		msg:"抱歉,保存失败!",
		okButton:"ok",
		okCallback:function(){
			console.log("To:http://www.baidu.com");
		}
    });
	dialog.show();
}

//以下是使用demo4
function showAlert4(msg) {
	var dialog = new ShowAlertDialog({
		icon:"info",
    	title:"提示框",
		msg:msg,
		okButton:"确定"
    });
	dialog.show();
}

function showConfirmMsg(msg) {
	var dialog = new ShowAlertDialog({
		msg:msg,
    });
	dialog.show();
}